package Test.tow;

//2、需求：用Set集合存储自定义对象并遍历Student(name,age)
//        分析：
//        A:创建学生类
//        B:创建集合对象
//        C:创建学生对象
//        D:把学生对象添加到集合对象中
//        E:遍历集合（使用迭代器和增强for两种遍历方式）

import java.util.HashSet;
import java.util.Iterator;

class student{
    String name;
    public student(String name){
        this.name = name;
    }
    public String toString(){
        return name;
    }
}
public class Set {
    public static void main(String[] args) {
        student s1 = new student("jack");
        student s2 = new student("lucy");
        student s3 = new student("james");
        student s4 = new student("tony");
        HashSet hset = new HashSet();
        hset.add(s1);
        hset.add(s2);
        hset.add(s3);
        hset.add(s4);

        Iterator it = hset.iterator();
        while(it.hasNext()){
            Object obj = it.next();
            System.out.print(obj+" ");
        }
        System.out.println();

        for(Object obj : hset){
            System.out.print(obj+" ");
        }
        System.out.println();
    }
}
